﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Configuration;

namespace RpgEditor
{
    static class Program
    {
        public const string PreviouslyLoadedGamePathConfigName = "PreviouslyLoadedGamePath";
        public const string NOPATH = "NOPATH";

        private static Configuration config = null;
        public static Configuration Config
        {
            get
            {
                if (config == null)
                    config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                return config;
            }
        }

        public static string PreviouslyLoadedGamePath
        {
            get
            {
                return LoadPreviousGamePath();
            }
            set
            {
                if (Config.AppSettings.Settings[PreviouslyLoadedGamePathConfigName].Value == null)
                    Config.AppSettings.Settings.Add(PreviouslyLoadedGamePathConfigName, value);
                else
                    Config.AppSettings.Settings[PreviouslyLoadedGamePathConfigName].Value = value;

                SaveConfigurationFile();
            }
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormMain());
        }

        static string LoadPreviousGamePath()
        {
            if (Config.AppSettings.Settings[PreviouslyLoadedGamePathConfigName] == null ||
                Config.AppSettings.Settings[PreviouslyLoadedGamePathConfigName].Value == null)
                Config.AppSettings.Settings.Add(PreviouslyLoadedGamePathConfigName, string.Empty);

            return Config.AppSettings.Settings[PreviouslyLoadedGamePathConfigName].Value;            
        }

        public static void SaveConfigurationFile()
        {
            Config.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection("appSettings");
        }

    }
}
